const isPrime = [];
for (let i = 0; i < 1000; i++) {
  isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
for (let i = 2; i < 1000; i++) {
  if (!isPrime[i]) {
    continue;
  }
  for (let j = i + i; j < 1000; j += i) {
    isPrime[j] = false;
  }
}

const solve = function(b) {
  const h = b.length;
  const w = b[0].length;
  for (let x = 0; x < w; x++) {
    for (let y = 0; y < h; y++) {
      if (isPrime[parseInt(b[y][x])]) {
        b[y][x] = 2;
      } else {
        b[y][x] = 0;
      }
    }
  }
  console.log('');
  for (let y = 0; y < h; y++) {
    let line = '';
    for (let x = 0; x < w; x++) {
      line += ' ' + b[y][x];
    }
    console.log(line);
  }
  for (let y = 0; y < h; y++) {
    let indicesAfterFound = null;
    for (let x = 0; x < w; x++) {
      if (b[y][x] === 2 && indicesAfterFound === null) {
        indicesAfterFound = [];
      } else if (b[y][x] === 2) {
        for (const i in indicesAfterFound) {
          b[y][indicesAfterFound[i]] = 1;
        }
        indicesAfterFound = [];
      } else if (indicesAfterFound !== null) {
        indicesAfterFound.push(x);
      }
    }
  }
  for (let x = 0; x < w; x++) {
    let indicesAfterFound = null;
    for (let y = 0; y < h; y++) {
      if (b[y][x] === 2 && indicesAfterFound === null) {
        indicesAfterFound = [];
      } else if (b[y][x] === 2) {
        for (const i in indicesAfterFound) {
          if (b[indicesAfterFound[i]][x] === 1) {
            b[indicesAfterFound[i]][x] = 3;
          }
        }
        indicesAfterFound = [];
      } else if (indicesAfterFound !== null) {
        indicesAfterFound.push(y);
      }
    }
  }
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      if (b[y][x] === 1) {
        b[y][x] = 0;
      }
    }
  }
  console.log('');
  for (let y = 0; y < h; y++) {
    let line = '';
    for (let x = 0; x < w; x++) {
      if (b[y][x] === 3) {
        line += ' *';
      } else {
        line += ' ' + b[y][x];
      }
    }
    console.log(line);
  }
};

var readline = require('readline');
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});

const board = [];
rl.on('line', function(line){
  if (line.indexOf('EOF') !== -1) {
    solve(board);
    rl.close();
  } else {
    line = line.split(' ');
    board.push(line);
  }
});
